<template>
    <div id="backGround">
        <Spin size="large" fix v-if="Loading"></Spin>
    </div>
</template>

<script>
    import {postAction, getAction} from '@/api/request'
    import cookie from 'js-cookie'
    import {mapActions} from 'vuex'
    import CryptoJS from 'crypto-js'

    export default {
        name: "without-code-login",

        data() {
            return {
                url: "/api/ac/subsistence/messageLoginServiceImpl/withoutCodeLogin",
                Loading: true,
            }
        },
        methods: {
            ...mapActions([
                'getUserInfo'
            ]),
            getUrlKey(name) {//获取url 参数
                return decodeURIComponent(
                    (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
            },
            async beforeLogin(key) {
                let rest = await this.getUserInfo();
                if (rest.errcode === 0) {
                    cookie.set("setSeverUserInfo", rest.data);
                    cookie.set("token", key);
                    // this.Loading = false;
                    setTimeout(() => {
                        this.$router.push({
                            name: this.$config.homeName
                        });
                    }, 500);
                } else {
                    this.$Message.error("登录失败！");
                    this.Loading = false;
                    this.$router.push({path: '/login'});
                }
            },
            // AES 加密方法
            encrypt(word) {
                // 和后端沟通后写死key和偏移量
                let key = CryptoJS.enc.Utf8.parse('asd8a6218921h123');
                let iv = CryptoJS.enc.Utf8.parse('iv_login_p3XxcC8');
                let srcs = CryptoJS.enc.Utf8.parse(word);
                var encrypted = CryptoJS.AES.encrypt(srcs, key, {
                    iv: iv,
                    mode: CryptoJS.mode.CBC,
                    padding: CryptoJS.pad.ZeroPadding
                });
                return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
            },
        },
        created() {
            var loginName = this.getUrlKey('loginname');
            var password = this.encrypt(this.getUrlKey('password'));
            postAction(this.url, {username: loginName, password: password}).then((res) => {
                let data = res.data.data;
                if (res.data && data) {
                    if (data.errcode === 0) {
                        this.beforeLogin(data.key);
                    } else {
                        this.$Message.error(data.errmsg);
                        this.Loading = false;
                        this.$router.push({path: '/login'});
                    }
                } else {
                    this.$Message.error("登录失败！");
                    this.Loading = false;
                    this.$router.push({path: '/login'});
                }
            })
        }
    }
</script>

<style scoped>
    #backGround {
        background-image: url(./login-bg.jpg);
        width: 100%;
        height: 100%;
    }
</style>